<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="生产制作单编号" prop="moCode">
        <el-input
          v-model="queryParams.moCode"
          placeholder="请输入生产制作单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划编码" prop="planCode">
        <el-input
          v-model="queryParams.planCode"
          placeholder="请输入计划编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-table border v-loading="loading" :data="manufactureorderList" @selection-change="handleSelectionChange">
      <el-table-column label="生产制作单编号" align="center" prop="moCode" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">{{ scope.row.moCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="计划名称" align="center" prop="planName" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="计划编码" align="center" prop="planCode" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="工单名称" align="center" prop="workorderName" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="客户名称" align="center" prop="clientName" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="来源类型" align="center" prop="orderSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_workorder_sourcetype" :value="scope.row.orderSource"/>
        </template>
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="productCode" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="productSpc" width="150"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" width="60"/>
      <el-table-column label="计划数量" align="center" prop="quantity" width="100"/>
      <el-table-column label="需求日期" align="center" prop="requestDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开工日期" align="center" prop="startDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划完工日期" align="center" prop="endDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="80px">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-printer"
              @click="handleView(scope.row)"
            >打印
            </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <work-sheet v-if="infoOpen" ref="worksheet"></work-sheet>
    </el-card>
  </div>
</template>

<script>
import { addManufactureorder, delManufactureorder, getManufactureorder, listManufactureorder, updateManufactureorder } from "@/api/mes/pro/manufactureorder";
import WorkSheet from "@/components/manufactureorder/worksheet.vue";

export default {
  name: "Manufactureorder",
  components: {WorkSheet},
  dicts: ['mes_workorder_sourcetype'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产制作单表格数据
      manufactureorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moCode: null,
        moName: null,
        planCode: null,
        planName: null,
        workorderCode: null,
        workorderName: null,
        productCode: null,
        productName: null,
        clientCode: null,
        clientName: null,
        vendorCode: null,
        vendorName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产制作单列表 */
    getList() {
      this.loading = true;
      listManufactureorder(this.queryParams).then(response => {
        this.manufactureorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moId: null,
        moCode: null,
        moName: null,
        planCode: null,
        planName: null,
        workorderCode: null,
        workorderName: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        quantityProduced: null,
        itemOrProduct: null,
        contractCode: null,
        contractName: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        startDate: null,
        endDate: null,
        requestDate: null,
        status: "0",
        deptName: null,
        lister: null,
        auditor: null,
        approver: null,
        moTime: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.moId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产制作单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const moId = row.moId || this.ids
      getManufactureorder(moId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产制作单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          if (this.form.moId != null) {
            updateManufactureorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManufactureorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const moIds = row.moId || this.ids;
      this.$modal.confirm('是否确认删除生产制作单编号为"' + moIds + '"的数据项？').then(function () {
        return delManufactureorder(moIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/manufactureorder/export', {
        ...this.queryParams
      }, `manufactureorder_${new Date().getTime()}.xlsx`)
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      this.infoOpen = true;
      this.$nextTick(() => {
        this.$refs.worksheet.init(row.moId);
      });
    },
  }
};
</script>
